const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin') 
const MyPlugin = require('./plugin.js')
const { glob } = require('glob');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');

const html = ['index.html','index2.html'];
let files = glob.sync('src/**/*.html');
let htmls = files.map(res => {
  let template = res.replace(/\\/g,'/');
  let filename = template.replace('src/','');
  return new HtmlWebpackPlugin({
    publicPath : "https://s.hangjiayun.com/allxcx/cailiao/dist/",
    template: template,
    filename: filename,
    inject : 'body',
    minify : false,
  })
})
module.exports = {
  mode : 'production',
  entry : ['core-js','./src/js/main.js'],
  output : {
    path: path.join(__dirname, "dist"),
    filename: "bundle_[chunkhash:8].js",
    clean : true,
    //environment : {
      //arrowFunction : false,
    //}
  },
  plugins: [
    //new webpack.ProgressPlugin(),
    new MiniCssExtractPlugin({
      filename:'[name]_[chunkhash:8].css'
    }),
    new webpack.ProvidePlugin({
      $ : 'jquery',
      jQuery : 'jquery',
      'window.jQuery' : 'jquery',
      // ...
    }),
    ...htmls,
    //new CopyWebpackPlugin({
      //patterns: [
        //{ from: "*.html", to: path.join(__dirname,"build"),context : path.resolve(__dirname,'dist'), force : true },
      //],
    //}),
    new MyPlugin({ options: "" }),
  ],
  module: { 
    rules: [
      {
        test: /\.png|\.jpe?g|\.gif|\.svg|\.ttf|\.eot|\.woff2|\.woff|\.mp4$/,
        use: [{
          loader : 'file-loader',
          options : {
            outputPath: 'images',
            esModule: false
          }
        }],
      },
      {
        test: /\.css$/,
        use: [ MiniCssExtractPlugin.loader,"css-loader",
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  [
                    'postcss-preset-env',
                    {
                      // 其他选项
                    },
                  ],
                  ['postcss-flexibility']
                ],
              },
            },
          },
        ]
      },
      {
        test: /\.scss$/,
        use: [  MiniCssExtractPlugin.loader,"css-loader",{
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  [
                    'postcss-preset-env',
                    {
                      // 其他选项
                    },
                  ],
                  ['postcss-flexibility']
                ],
              },
            },
          },"sass-loader",
        ]
      },
      {
        test : /\.js$/,
        //exclude: /node_modules/, // 排除依赖里的代码
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',{
                    "useBuiltIns": "entry",
                    "corejs" : "2.6.12"
                  }
                ]
              ],
              //plugins: ['@babel/plugin-transform-runtime']
            }
          },
        ]
      },
      //{
        //test: /\.html|\.tpl$/i,
        //use : {
          //loader: 'html-withimg-loader',
          //options : {
            //sources : {
              //list : [
                //'...',
                //{
                  //tag: 'img',
                  //attribute: 'data-original',
                  //type: 'src',
                //}
              //]
            //}
          //}
        //}
      //},
    ]
  },
  resolve : {
    alias : {
      '@' : path.resolve(__dirname,'src')
    }
  },
};
